package com.partsoft.dits.mq;

import org.apache.commons.pool.KeyedPoolableObjectFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;

import com.ibm.mq.MQQueueManager;

public class MQQMPoolFactory implements KeyedPoolableObjectFactory, InitializingBean {
	
	private MQConnectFactory factory = null;
	
	public MQQMPoolFactory() {}
	
	public MQQMPoolFactory(MQConnectFactory factory ){
		setFactory(factory);
	}
	
	public void setFactory(MQConnectFactory factory) {
		Assert.notNull(factory);
		this.factory = factory;
	}
	
	public void activateObject(Object key, Object qm) throws Exception {
		//
	}

	public void destroyObject(Object arg0, Object qm) throws Exception {
		MQQueueManager mqqm = (MQQueueManager)qm;
		mqqm.disconnect();
	}

	public Object makeObject(Object key) throws Exception {
		return factory.getQueueManager((String)key);
	}

	public void passivateObject(Object key, Object qm) throws Exception {
	}

	public boolean validateObject(Object key, Object qm) {
		MQQueueManager mqqm = (MQQueueManager)qm;
		return  mqqm.isOpen();
	}

	public void afterPropertiesSet() throws Exception {
		Assert.notNull(factory, "必须设置MQ连接工厂");
	}
}
